/**
 * Created by fongwell on 2017/2/25.
 */
let timeout = 800,
    currentCenter = null,
    viewStack = [],
    ALL_CLASSES = 'page-on-center page-on-left page-on-right page-on-bottom';

export class Transition {
    constructor(content) {
        this.content = content;
        viewStack.push(this.content);
        currentCenter = this.content;
    }
    rightToCenter(node) {
        viewStack.push(node);
        this._transiton(currentCenter, 'centerToLeft', node, 'rightToCenter');
        currentCenter = node;
    }
    centerToRight(node) {
        this._backToCenter();
        this._transiton(currentCenter, 'leftToCenter', node, 'centerToRight');
    }
    bottomToCenter(node) {
        viewStack.push(node);
        this._transiton(node, 'bottomToCenter');
        currentCenter = node;
    }
    centerToBottom(bottom) {
        this._backToCenter();
        this._transiton(bottom, 'centerToBottom');
    }
    _backToCenter() {
        currentCenter = viewStack.pop();

        if (viewStack.length === 0) {
            currentCenter = this.content;
            viewStack = [currentCenter];
        } else {
            currentCenter = viewStack[viewStack.length - 1];
        }
    }
    _transiton(node1, dir1, node2, dir2) {
        let self = this;
        this.transition(node1, dir1);

        if (node2) {
            setTimeout((function () {
                self.transition(node2, dir2);
            })(), 0);
        }
    }
    transition(node, dir) {
        node.removeClass(ALL_CLASSES).addClass('sliding');
        var transition = Transition.transitions[dir];
        if (!transition) {
            throw new Error('transition ' + dir + ' not found!');
        }
        transition.call(this, this, node);
    }
    doTransition(timeout, fn) {
        let self = this;

        let fnWrap = ((context, fn) => {
            return function () {
                fn.call(context);
            };
        })(self, fn);

        self.timeout = setTimeout(fnWrap, timeout);
    }
    clearTimer() {
        clearTimeout(this.timeout)
    }
}

Transition['transitions'] = {
    centerToLeft: function (instance, node) {
        node.addClass('center-to-left');
        instance.doTransition(timeout, function () {
            node.addClass('page-on-left').removeClass('center-to-left sliding');
        });
    },
    leftToCenter: function (instance, node) {
        node.addClass('left-to-center');
        instance.doTransition(timeout, function () {
            node.addClass('page-on-center').removeClass('left-to-center sliding');
        });
    },
    rightToCenter: function (instance, node) {
        node.addClass('right-to-center');
        instance.doTransition(timeout, function () {
            node.addClass('page-on-center').removeClass('right-to-center sliding');
        });
    },
    centerToRight: function (instance, node) {
        node.addClass('center-to-right');
        instance.doTransition(timeout, function () {
            node.addClass('page-on-right').removeClass('center-to-right sliding');
        });
    },
    bottomToCenter: function (instance, node) {
        node.addClass('bottom-to-center');
        instance.doTransition(timeout, function () {
            node.addClass('page-on-center').removeClass('bottom-to-center sliding');
        });
    },
    centerToBottom: function (instance, node, fn) {
        node.addClass('center-to-bottom');
        instance.doTransition(timeout, function () {
            node.addClass('page-on-bottom').removeClass('center-to-bottom sliding');
        });
    }
}